$(document).ready(function() {
	//Handle Default
	if(localStorage['pdfCrowdOptions'] == null) {
		localStorage['pdfCrowdOptions'] = $('input:radio[name=pdfCrowdOptions]').get(0).value;
	}
	if(localStorage['username'] == null) {
		localStorage['username'] = $('#username').get(0).value;
	}
	if(localStorage['token'] == null) {
		localStorage['token'] = $('#token').get(0).value;
	}
	
	var radios = $('input:radio[name=pdfCrowdOptions]');
	radios.filter('[value=' + localStorage['pdfCrowdOptions'] + ']').attr('checked', true);

	$('#username').get(0).value = localStorage['username'];
	$('#token').get(0).value = localStorage['token'];

	if($('#usePdfCrowdAccount').get(0).checked) {
		setCheckedState();
	}
	else {
		setUncheckedState();
	}

	$('input:radio[name=pdfCrowdOptions]').click(function(e) {
		if(this.id == "usePdfCrowdAccount") {
			setCheckedState('medium');
		}
		else {
			setUncheckedState('medium');
		}
	});
	
	$('#save-button').click(save);
});

function setCheckedState(speed) {
	$("#pdfCrowdOptions").show(speed);
	$('#username').removeAttr("disabled"); 
	$('#token').removeAttr("disabled");
}

function setUncheckedState(speed) {
	$("#pdfCrowdOptions").hide(speed);
	$('#username').attr("disabled", true); 
	$('#token').attr("disabled", true);
}

function save() {
	localStorage['pdfCrowdOptions'] = $('input:radio[name=pdfCrowdOptions]:checked').get(0).value;
	localStorage['username'] = $('#username').get(0).value;
	localStorage['token'] = $('#token').get(0).value;

	var status = document.getElementById("status");
	status.innerHTML = "Options Saved.";
	setTimeout(function() {
		status.innerHTML = "";
	}, 4000);
}